﻿// Simple menu class
Menu = function (title, items, y, size, width, callback, backgroundCallback) {
    this.title = title;
    this.items = items;    
    this.selectedItem = 0;
    this.callback = callback;
    this.y = y;
    this.size = size;
    this.width = width;
    this.backgroundCallback = backgroundCallback;
}

Menu.prototype.constructor = Menu;

Menu.prototype.Render = function (elapsed) {
    if (this.backgroundCallback)
        this.backgroundCallback(elapsed);
    else {
        var lingrad = _context.createLinearGradient(0, 0, 0, _tela.height);
        lingrad.addColorStop(0, '#fff');
        lingrad.addColorStop(1, '#0099cc');
        _context.fillStyle = lingrad;
        _context.fillRect(0, 0, _tela.width, _tela.height);
    }

    _context.textAlign = "center";
    
    var y = this.y;
    if (this.title) {
        _context.fillStyle = "#000";
        _context.font = Math.floor(this.size * 1.3).toString() + "px Verdana";
        _context.fillText(this.title, _tela.width / 2, y);
        y += this.size;
    }

    for (var i = 0; i < this.items.length; ++i) {
        _context.fillStyle = "White";
        var size = Math.floor(this.size * 0.8);

        if (i == this.selectedItem) {            
            _context.fillStyle = "rgba(255,255,10,255)";
            size = this.size;
        }

        _context.font = size.toString() + "px Verdana";
        y += this.size;
        _context.fillText(this.items[i], _tela.width / 2, y);
        _context.fillStyle = "White";
     }   
}

Menu.prototype.Input = function (lastMouseX, lastMouseY) {
	var prevSelected = this.selectedItem;
	var leftx = (_tela.width - this.width) / 2;

	if (lastMouseX >= leftx && lastMouseX < leftx + this.width) {
		var y = this.y + this.size * 0.2;

		if (this.title)
			y += this.size;
		if (lastMouseY >= y && lastMouseY < (y + this.size * this.items.length))
			this.selectedItem = Math.floor((lastMouseY - y) / this.size);
	}

	if (prevSelected != this.selectedItem) {
		//AudioManager.play("blip");
	}

	this.callback(this.selectedItem);
}	